widget: Add a translate_coordinates version for doubles
authorTimm Bäder <mail@baedert.org>
Wed, 7 Mar 2018 14:14:37 +0000 (15:14 +0100)
committerTimm Bäder <mail@baedert.org>
Wed, 7 Mar 2018 19:17:39 +0000 (20:17 +0100)
So we can use that one when translating event coordinates. Also adapt
the widgetfocus demo to ensure this works.

We should probably at some point delete either the int or the double
version.

gtk/gtkwidget.c
tests/testwidgetfocus.c

index 2b5fd88e2a047c83e55f68e760cfb0ab3c22948e..65bcf6adf5470665defebf716c5d31b2bf233f13 100644 (file)
@@ -4811,6 +4811,70 @@ gtk_widget_translate_coordinates (GtkWidget  *src_widget,
   return TRUE;
 }
 
+/* This is the same as translate_coordinates, but it works on doubles.
+ * We use this for event coordinates.
+ *
+ * We should probably decide for only one of the 2 versions at some point */
+static gboolean
+gtk_widget_translate_coordinatesf (GtkWidget  *src_widget,
+                                   GtkWidget  *dest_widget,
+                                   double      src_x,
+                                   double      src_y,
+                                   double     *dest_x,
+                                   double     *dest_y)
+{
+  GtkWidget *ancestor;
+  GtkWidget *parent;
+
+  g_return_val_if_fail (GTK_IS_WIDGET (src_widget), FALSE);
+  g_return_val_if_fail (GTK_IS_WIDGET (dest_widget), FALSE);
+
+  ancestor = gtk_widget_common_ancestor (src_widget, dest_widget);
+  if (!ancestor)
+    {
+      if (dest_x)
+        *dest_x = 0;
+      if (dest_y)
+        *dest_y = 0;
+      return FALSE;
+    }
+
+
+  parent = src_widget;
+  while (parent != ancestor)
+    {
+      int origin_x, origin_y;
+
+      gtk_widget_get_origin_relative_to_parent (parent, &origin_x, &origin_y);
+
+      src_x += origin_x;
+      src_y += origin_y;
+
+      parent = _gtk_widget_get_parent (parent);
+    }
+
+  parent = dest_widget;
+  while (parent != ancestor)
+    {
+      int origin_x, origin_y;
+
+      gtk_widget_get_origin_relative_to_parent (parent, &origin_x, &origin_y);
+
+      src_x -= origin_x;
+      src_y -= origin_y;
+
+      parent = _gtk_widget_get_parent (parent);
+    }
+
+  if (dest_x)
+    *dest_x = src_x;
+
+  if (dest_y)
+    *dest_y = src_y;
+
+  return TRUE;
+}
+
 static void
 gtk_widget_real_size_allocate (GtkWidget           *widget,
                                const GtkAllocation *allocation,
@@ -5750,17 +5814,17 @@ translate_event_coordinates (GdkEvent  *event,
 {
   GtkWidget *event_widget;
   double x, y;
-  int dx, dy;
+  double dx, dy;
 
   if (!gdk_event_get_coords (event, &x, &y))
     return;
 
   event_widget = gtk_get_event_widget (event);
 
-  gtk_widget_translate_coordinates (event_widget,
-                                    widget,
-                                    x, y,
-                                    &dx, &dy);
+  gtk_widget_translate_coordinatesf (event_widget,
+                                     widget,
+                                     x, y,
+                                     &dx, &dy);
 
   gdk_event_set_coords (event, dx, dy);
 }
index 2366e7c10129a02ef883416b98954757629b6b5d..3aed9e737675d361f3515d6ce6c604765a59651b 100644 (file)
@@ -63,8 +63,8 @@ const char *css =
 struct _GtkFocusWidget
 {
   GtkWidget parent_instance;
-  int mouse_x;
-  int mouse_y;
+  double mouse_x;
+  double mouse_y;
 
   union {
     struct {
@@ -188,7 +188,7 @@ gtk_focus_widget_snapshot (GtkWidget *widget, GtkSnapshot *snapshot)
                                  "Crosshair 2");
 
       layout = gtk_widget_create_pango_layout (widget, NULL);
-      text = g_strdup_printf ("%d×%d", self->mouse_x, self->mouse_y);
+      text = g_strdup_printf ("%.2f×%.2f", self->mouse_x, self->mouse_y);
       pango_layout_set_text (layout, text, -1);
 
       gtk_snapshot_render_layout (snapshot,
@@ -207,7 +207,7 @@ gtk_focus_widget_event (GtkWidget *widget,
                         GdkEvent  *event)
 {
   GtkFocusWidget *self = GTK_FOCUS_WIDGET (widget);
-  gdouble x, y;
+  double x, y;
 
   if (gdk_event_get_event_type (event) == GDK_MOTION_NOTIFY)
     {